package com.mes.eventhandler;

import java.util.List;

import javax.enterprise.event.Observes;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;

import com.mes.quality.data.EmesqInspectionGroupItem;

public class EmesqInspectionGroupItemEventHandler extends EntityPersistenceEventObserver {

	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EmesqInspectionGroupItem.ENTITY_NAME) };

	protected Logger logger = Logger.getLogger(this.getClass());

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}
	
	private void throwMessage(String message){
		String language = OBContext.getOBContext().getLanguage().getLanguage();
		ConnectionProvider conn = new DalConnectionProvider(false);
		throw new OBException(Utility.messageBD(conn, message, language));
	}
	
	public void onSave(@Observes EntityNewEvent event) {
	    if (!isValidEvent(event)) {
	      return;
	    }
	    final EmesqInspectionGroupItem item = (EmesqInspectionGroupItem) event.getTargetInstance();
	    StringBuffer sql = new StringBuffer();
	    sql.append("select 1 from Emesq_Inspection_Group_Item item,Emesq_Inspection_Group g ");
	    sql.append("	where item.Emesq_Inspection_Group_ID = g.Emesq_Inspection_Group_ID ");
	    sql.append("		and item.Emesq_Inspection_Item_ID= ? ");
	    sql.append("		and g.Code= ? ");
        Query query = OBDal.getInstance().getSession().createSQLQuery(sql.toString());
        query.setParameter(0, item.getEmesqInspectionItem().getId());
        query.setParameter(1, item.getEmesqInspectionGroup().getCode());
        List<Object> list = query.list();
        if(list.size() > 0 ){//说明 code 和  emesqInspectionItem有同样的数据
        	this.throwMessage("编码("+item.getEmesqInspectionGroup().getCode()+")已经存在相同的检验项目");
        }
	}

	public void onUpdate(@Observes EntityUpdateEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		final EmesqInspectionGroupItem item = (EmesqInspectionGroupItem) event.getTargetInstance();
	    StringBuffer sql = new StringBuffer();
	    sql.append("select 1 from Emesq_Inspection_Group_Item item,Emesq_Inspection_Group g ");
	    sql.append("	where item.Emesq_Inspection_Group_ID = g.Emesq_Inspection_Group_ID ");
	    sql.append("		and item.Emesq_Inspection_Item_ID= ? ");
	    sql.append("		and g.Inspection_Group_Code= ? ");
	    sql.append("		and item.Emesq_Inspection_Group_Item_ID <> ? ");
	    
        Query query = OBDal.getInstance().getSession().createSQLQuery(sql.toString());
        query.setParameter(0, item.getEmesqInspectionItem().getId());
        query.setParameter(1, item.getEmesqInspectionGroup().getCode());
        query.setParameter(2, item.getId());
        try {
        List<Object> list = query.list();
        if(list.size() > 0 ){//说明 code 和  emesqInspectionItem有同样的数据
        	this.throwMessage("编码("+item.getEmesqInspectionGroup().getCode()+")已经存在相同的检验项目");
        }
        } catch (Exception e) {
			// TODO: handle exception
        	e.printStackTrace();
		}
	}

}
